<?php
/**
 * 活动小程序相关服务类
 * Actutils.php BY My
 * Created by sen at 2021/3/23
 * AgeClub.net @ 2021
 */

namespace app\api\controller\bzh\product;


use app\admin\model\moyi\log\Like;
use app\api\library\Utils;
use app\common\controller\Api;
use think\Db;
use think\Log;
use think\Validate;
use think\Cache;

class Product extends Api
{
    protected $noNeedLogin = ['productList','productHot','productDetail','productType','searchDetail','taxonomy','productMore','searchDetail'];
    protected $noNeedRight = '*';



//    产品列表页面
//$page 翻页 $type 分类
    public function productList()
    {
        if ($this->request->isGet()) {
            $type = $this->request->request('type', 1);
            $page = $this->request->request('page', 1);
            $total = Db::name('bzh_product')
                ->where('status', 'normal')
                ->where('taxonomy', $type)
                ->count();

            $limit = ($page-1) * 5;
            $list = Db::name('bzh_product')
                ->where('taxonomy', $type)
                ->where('status', 'normal')
                ->limit($limit, 5)
                ->field('id,name,describe,proimage,introduce')
                ->select();

            if(empty($list)){
                $this->error('暂无分类该更多产品',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }
    /**
     * 获取展会热门产品列表
     */
    public function productHot()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_product')
                ->where('status', 'normal')
                ->where('switch', 1)
                ->limit(0, 3)
                ->field('id,name,describe,proimage,introduce')
                ->select();

            $result = array("rows" => $list);
            if (!empty($list)) {
                $this->success('获取信息成功', $result, 200);
            } else {
                $this->error('暂未设置热门资讯', 406);
            }
        }

    }


    public function productDetail()
    {
        if ($this->request->isGet()) {
            $id = $this->request->request('id', '');
            $user_id = $this->request->request('user_id', '');

            Db::name('bzh_product')->where('id', $id)->setInc('hit');//点击加一
            $total = Db::name('bzh_product')
                ->where('id', $id)
                ->count();

            if ($total == '1') {
                $list = Db::name('bzh_product')
                    ->where('id', $id)
                    ->select();
            } else {
                $this->error('暂无该产品', '', 3010);
            }

//            var_dump($list[0]['business_id']);var_dump($user_id);die;
//            $list[0]['business_id']=1;
            $guanzhu = Db::name('bzh_attention')
                ->where('user_id',$user_id)
                ->where('lei',1)
                ->where('gx_id',$list[0]['business_id'])
                ->where('status','normal')
                ->count();


            if($guanzhu =='1'){
                $state='1';//已关注
//
            }else{
                $state='0';//未关注
//
            }
            $list = str_replace("img",'img width="100%"',$list);
            $list = collection($list)->toArray();
            $result = array("rows" => $list,"state"=>$state);
            $this->success('获取产品信息成功', $result, 200);
        }
    }




    public function productMore()
    {
        if ($this->request->isGet()) {
            $business_id = $this->request->request('business_id', '');
            $hot = $this->request->request('hot', '30');
            $total = Db::name('bzh_product')
                ->where('business_id', $business_id)
                ->count();

            if ($total > '0') {
                $list = Db::name('bzh_product')
                    ->where('business_id', $business_id)
                    ->limit($hot)
                    ->select();
            } else {
                $this->error('暂无该产品', '', 3010);
            }
            $business_info ='';
            if(!empty($business_id)){
                $business_info = Db::name('bzh_business')
                    ->where('id', $business_id)
                    ->field('id,log,company,city,hit')
                    ->find();
            }


            $list = collection($list)->toArray();
            $business_info = collection($business_info)->toArray();
            $result = array("rows" => $list,"business_info" =>$business_info);
            $this->success('商户产品信息成功', $result, 200);
        }
    }




    /**
     * 获取展会搜索结果
     */
    /**
     * 获取展会搜索结果
     */
    public function searchDetail()
    {
        if ($this->request->isGet()) {
            $product = $this->request->request('product', '');
            if(!$product){
                $this->error('请输入搜索条件', '', 3010);
            }

            $total = Db::name('bzh_product')
                ->where('name', 'like', "%$product%")
                ->count();

            if ($total == '0') {
                $this->error('暂无搜索结果', '', 3010);
            } else {
                $list = Db::name('bzh_product')
                    ->where('name', 'like', "%$product%")
                    ->field('id,name,proimage,introduce')
                    ->select();
            }


            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('索搜成功', $result, 200);

        }
    }

    /**
     * 获取展会搜索结果
     *
     */
    public function taxonomy()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_taxonomy')
                ->field('id,taxonomy')
                ->select();
            if(empty($list)){
                $this->error('获取分类失败', '', 3010);
            }
            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('获取分类信息成功', $result, 200);
        }
    }




}
